package com.king.tv.mvp.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.king.tv.App;
import com.king.tv.R;
import com.king.tv.bean.Video;
import com.king.tv.http.ApiConfig;
import com.king.tv.http.BaseCallModel;
import com.king.tv.http.HttpLoader;
import com.king.tv.util.RoundedCornersTransformation2;
import com.pili.pldroid.player.PLOnCompletionListener;
import com.pili.pldroid.player.PLOnErrorListener;
import com.pili.pldroid.player.PLOnPreparedListener;
import com.pili.pldroid.player.widget.PLVideoView;
import com.zhangqie.zqvideolibrary.ZQVideoPlayer;
import com.zhangqie.zqvideolibrary.ZQVideoPlayerStandard;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
 * @since 2017/3/3
 */

public class EasynewLiveVideoAdapter extends RecyclerArrayAdapter<Video> {

    private boolean isShowStatus;

    private Context context;
    public EasynewLiveVideoAdapter(Context context, List<Video> objects, boolean isShowStatus) {
        super(context, objects);
        this.isShowStatus = isShowStatus;
        this.context = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new LiveViewHolder(parent);
    }

    public class LiveViewHolder extends BaseViewHolder<Video>{

        ZQVideoPlayerStandard zqVideoPlayerStandard;

        public LiveViewHolder(ViewGroup parent) {
            super(parent,R.layout.item_videoview);
            zqVideoPlayerStandard = $(R.id.video_player);
        }

        @Override
        public void setData(final Video data) {
            super.setData(data);
            //设置图片圆角角度
            Log.i("tag","=============url"+data.getImg());
            if(!TextUtils.isEmpty(data.getImg())){
                String url = ApiConfig.BASE_URL+data.getImg();
                Log.i("tag","=============url"+url);
                //设置图片圆角角度
//                Glide.with(getContext()).load(url).error(R.mipmap.live_default).transform(new RoundedCornersTransformation2(10, 0, RoundedCornersTransformation2.CornerType.ALL, RoundedCornersTransformation2.ScaleType.CENTER_CROP)).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(zqVideoPlayerStandard.thumbImageView);
                    Glide.with(getContext()).load(url).error(R.mipmap.live_default).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(zqVideoPlayerStandard.thumbImageView);
            }else{
                Glide.with(getContext()).load(R.mipmap.live_default).error(R.mipmap.live_default).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(zqVideoPlayerStandard.thumbImageView);
//                Glide.with(getContext()).load(R.mipmap.live_default).transform(new RoundedCornersTransformation2(10, 0, RoundedCornersTransformation2.CornerType.ALL, RoundedCornersTransformation2.ScaleType.CENTER_CROP)).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(zqVideoPlayerStandard.thumbImageView);
            }
//            if(TextUtils.isEmpty(data.getUser().get))
//            tvTitle.setText(data.getTypeNote());
//            tvName.setText(data.getTitle());
            int num = (int)(1+Math.random()*(2000)+1000);
//            tvViewer.setText(num+"");
            Video.UserBean userBean = data.getUser();
            if(userBean!=null){
//                tvTitle.setText(data.getTitle());
//                tvName.setText(userBean.getUsercode());
//                Glide.with(getContext()).load(R.mipmap.ic_launcher).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(ivitemlive);
            }
//            vtv.start();
                    HttpLoader.getInstance().getMovieurl(data.getObjectname()).subscribe(new Observer<BaseCallModel<String>>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }
                        @Override
                        public void onNext(BaseCallModel<String> value) {
                            //设置视频地址和标题
                            zqVideoPlayerStandard.setUp(value.getData(), ZQVideoPlayer.SCREEN_WINDOW_LIST, data.getTitle());
                            Log.i(App.TAG,"========url"+value.getData());
//                            iv.setVisibility(View.INVISIBLE);
//                            vtv.setVisibility(View.VISIBLE);
//                            llbottom.setVisibility(View.INVISIBLE);
//                            vtv.setDisplayAspectRatio(PLVideoView.ASPECT_RATIO_16_9);
//                            vtv.setVideoPath(value.getData());
//                            vtv.setOnErrorListener(new PLOnErrorListener() {
//                                @Override
//                                public boolean onError(int i) {
//                                    Log.i(App.TAG,"========url"+i);
//                                    return false;
//                                }
//                            });
//                            vtv.setOnCompletionListener(new PLOnCompletionListener() {
//                                @Override
//                                public void onCompletion() {
//                                    iv.setVisibility(View.VISIBLE);
//                                    vtv.setVisibility(View.GONE);
//                                    llbottom.setVisibility(View.VISIBLE);
//                                    vtv.stopPlayback();
//                                }
//                            });
//                            vtv.setOnPreparedListener(new PLOnPreparedListener() {
//                                @Override
//                                public void onPrepared(int i) {
//                                    Log.i(App.TAG,"========url"+i);
//                                }
//                            });
//                            vtv.start();
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });

        }
    }
    public  int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
